package com.travelsky.flash.util.filter;

import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.travelsky.flash.constant.ServiceDeployConfigConstant;
import com.travelsky.flash.util.BaseUtil;
import com.travelsky.flash.vo.UserVO;

/**
 * @Description 
 * @author syuan
 * @date 2015年6月5日  上午9:36:43
 * Company: TravelSky
 */
public class AuthInterceptor implements HandlerInterceptor {
	
	private static ResourceBundle exceptUrl = ResourceBundle.getBundle("authExceptUrl",
			Locale.US);
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler) throws Exception {
		String requestUrl = request.getRequestURI();
		requestUrl = requestUrl.substring(1);
		requestUrl = requestUrl.substring(requestUrl.indexOf("/"));
		// 在排除之外
		for (String key : exceptUrl.keySet()) {
			if (requestUrl.equals(exceptUrl.getObject(key))) {
				return true;
			}
		}
		// user session 有值
		UserVO user = (UserVO)request.getSession().getAttribute(ServiceDeployConfigConstant.USER_SESSION);
		if(null != user && BaseUtil.stringNotNull(user.getName())) {
			return true;
		}
		response.sendRedirect("toLogin.do");
		return false;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response,
			Object handler, ModelAndView modelAndView) throws Exception {
		
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
			Object handler, Exception ex) throws Exception {
		
	}

}
